<template>
	<md-card style="margin:8px 0;">
		<md-card-header md-alignment="space-between" style="display:flex;">
			<span
				class="md-title md-title-text"
				:title="itemName+getLangText(label.itemStyle)"
			>{{itemName}}{{getLangText(label.itemStyle)}}</span>
			<span style="flex:1;text-align:right;">
				<md-button class="md-icon-button md-accent" @click="sendStyle" v-if="showItem">
					<i class="iconfont iconsave"></i>
				</md-button>
				<md-button
					:class="['md-icon-button',isShow?'md-primary':'']"
					@click="sendStyle"
					v-if="!showItem"
				>
					<i :class="['iconfont', isShow?'iconsave':'iconshezhi2']"></i>
				</md-button>
			</span>
		</md-card-header>
		<md-card-content v-if="isShow">
			<div class="table-row-item">
				<span>{{getLangText(label.color)}}:</span>
				<span>
					<span
						class="color-span"
						:style="{background:itemStyle.color}"
						@click="showColorPicker('color')"
					></span>
					{{itemStyle.color}}
				</span>
			</div>

			<color-picker
				:lang-type="langType"
				:rgba="itemStyle.color"
				@color-result="onColorPicker"
				:show-picker="picker.show&&picker.current=='color'"
			></color-picker>

			<md-field>
				<label>{{getLangText(label.borderType)}}</label>
				<md-select v-model="itemStyle.borderType">
					<md-option :value="item.en" v-for="item in types" :key="item.en">{{getLangText(item)}}</md-option>
				</md-select>
			</md-field>

			<md-field>
				<label>{{getLangText(label.borderWidth)}}</label>
				<md-input v-model="itemStyle.borderWidth"></md-input>
			</md-field>

			<div class="table-row-item">
				<span>{{getLangText(label.borderColor)}}:</span>
				<span>
					<span
						class="color-span"
						:style="{background:itemStyle.borderColor}"
						@click="showColorPicker('borderColor')"
					></span>
					{{itemStyle.borderColor}}
				</span>
			</div>

			<color-picker
				:lang-type="langType"
				:rgba="itemStyle.borderColor"
				@color-result="onColorPicker"
				:show-picker="picker.show&&picker.current=='borderColor'"
			></color-picker>

			<md-field>
				<label>{{getLangText(label.opacity)}}</label>
				<md-input v-model="itemStyle.opacity"></md-input>
			</md-field>
			<md-field v-if="('borderRadius' in itemStyle)">
				<label>{{getLangText(label.borderRadius)}}</label>
				<md-input v-model="itemStyle.borderRadius"></md-input>
			</md-field>

			<md-field>
				<label>{{getLangText(label.shadowBlur)}}</label>
				<md-input v-model="itemStyle.shadowBlur"></md-input>
			</md-field>

			<div class="table-row-item">
				<span>{{getLangText(label.shadowColor)}}:</span>
				<span>
					<span
						class="color-span"
						:style="{background:itemStyle.shadowColor}"
						@click="showColorPicker('shadowColor')"
					></span>
					{{itemStyle.shadowColor}}
				</span>
			</div>

			<color-picker
				:lang-type="langType"
				:rgba="itemStyle.shadowColor"
				@color-result="onColorPicker"
				:show-picker="picker.show&&picker.current=='shadowColor'"
			></color-picker>

			<md-field>
				<label>{{getLangText(label.shadowOffsetX)}}</label>
				<md-input v-model="itemStyle.shadowOffsetX"></md-input>
			</md-field>
			<md-field>
				<label>{{getLangText(label.shadowOffsetY)}}</label>
				<md-input v-model="itemStyle.shadowOffsetY"></md-input>
			</md-field>
		</md-card-content>
	</md-card>
</template>
<script>
	import ColorPicker from "@/components/comp/ColorPicker";
	export default {
		name: "ItemStyle",
		props: {
			langType: {
				type: String,
				default: window.localStorage.getItem("langType")
			},
			itemName: {
				type: String,
				default: ""
			},
			option: {
				type: Object,
				default: function() {
					return {};
				}
			},
			showItem: {
				type: Boolean,
				default: false
			}
		},
		components: {
			"color-picker": ColorPicker
		},
		data: () => ({
			label: {
				itemStyle: { zh: "数据项样式", en: "ItemStyle" },
				color: { zh: "图形颜色", en: "color" },
				borderType: { zh: "描边类型", en: "borderType" },
				borderWidth: { zh: "描边线宽", en: "borderWidth" },
				borderColor: { zh: "描边颜色", en: "borderColor" },
				shadowBlur: { zh: "阴影模糊大小", en: "shadowBlur" },
				shadowColor: { zh: "阴影颜色", en: "shadowColor" },
				shadowOffsetX: { zh: "阴影水平偏移", en: "shadowOffsetX" },
				shadowOffsetY: { zh: "阴影垂直偏移", en: "shadowOffsetY" },
				opacity: { zh: "图形透明度", en: "opacity" },
				borderRadius: { zh: "图形圆角", en: "borderRadius" }
			},
			isShow: false,
			itemStyle: {
				color: "",
				borderColor: "#000",
				borderType: "solid",
				borderWidth: 0,
				shadowBlur: "",
				shadowColor: "",
				shadowOffsetX: 0,
				shadowOffsetY: 0,
				opacity: 1
			},
			types: [
				{ zh: "实线", en: "solid" },
				{ zh: "虚线", en: "dashed" },
				{ zh: "点线", en: "dotted" }
			],
			picker: {
				show: false,
				current: ""
			},
			originOption: "",
			isParse: false
		}),
		watch: {			
			isShow() {
				if (this.isShow) {
					this.parseObj();
				} else {
					this.picker.show = false;
				}
			},
			itemStyle: {
				deep: true,
				handler() {
					if (this.isShow && !this.isParse)
						this.$emit("item-style-result", JSON.parse(JSON.stringify(this.itemStyle)));
				}
			}
		},
		methods: {
			showColorPicker(tag) {
				if (tag != this.picker.current || !this.picker.show) {
					this.picker.current = tag;
					this.picker.show = true;
				} else {
					this.picker.show = false;
				}
			},
			onColorPicker(op) {
				if (this.picker.current) this.itemStyle[this.picker.current] = op.rgba;
			},
			sendStyle() {
				if (!this.showItem) {
					this.isShow = this.isShow ? false : true;
					if (!this.isShow) {
						this.$emit("item-style-result", JSON.parse(JSON.stringify(this.itemStyle)));
					}
				} else {
					this.$emit("item-style-result", JSON.parse(JSON.stringify(this.itemStyle)));
				}
			},
			parseObj() {
				if(!this.option||JSON.stringify(this.option)=='{}')return;
				this.isParse = true;
				for (var key in this.option) {
					this.itemStyle[key] = this.option[key];
				}
				this.originOption = JSON.stringify(this.itemStyle);
				var that = this;
				setTimeout(() => {
					that.isParse = false;
				}, 100);
			},
			colseOption() {
				this.isShow = false;
				if (this.originOption != JSON.stringify(this.itemStyle)) {
					this.$emit("item-style-result", this.itemStyle);
				}
			},
			listenEvent() {
				this.app.$on("item-style-parse", this.parseObj);
				this.app.$on("close-option", this.colseOption);
			},
			offEvent() {
				this.app.$off("item-style-parse", this.parseObj);
				this.app.$off("close-option", this.colseOption);
			}
		},
		mounted() {
			this.isShow = this.showItem;
			this.listenEvent();
		},
		beforeDestroy() {
			this.offEvent();
		}
	};
</script>
